package com.pt25.base.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.pt25.base.exception.BizException;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Set;

import static com.pt25.base.config.JacksonConfig.objectMapper;


@Slf4j
public class JsonUtil {


    public static String toJson(Object obj) {
        if (null == obj) {
            return null;
        }
        if (obj instanceof String) {
            return (String) obj;
        }
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            log.error("toJson-error", e);
            throw new RuntimeException(e);
        }
    }

    public static <T> T toBean(String jsonString, Class<T> valueType) {
        try {
            if (null == jsonString) {
                return null;
            }
            return objectMapper.readValue(jsonString, valueType);
        } catch (Exception e) {
            log.error("fromJson-error", e);
            throw new RuntimeException(e);
        }
    }

    public static <T> T toBean(Object obj, Class<T> valueType) {
        if (null == obj) {
            return null;
        }
        if (obj instanceof String) {
            return toBean((String) obj, valueType);
        }
        return toBean(toJson(obj), valueType);
    }

    public static <T> List<T> toBeanList(String jsonString, Class<T> clazz) {
        if (null == jsonString) {
            return null;
        }
        try {
            CollectionType listType = objectMapper.getTypeFactory().constructCollectionType(List.class, clazz);
            return objectMapper.readValue(jsonString, listType);
        } catch (Exception e) {
            log.error("fromJsonToList-error", e);
            throw new RuntimeException(e);
        }
    }

    public static <T> List<T> toBeanList(Object obj, Class<T> clazz) {
        if (null == obj) {
            return null;
        }
        if (obj instanceof String) {
            return toBeanList((String) obj, clazz);
        }
        return toBeanList(toJson(obj), clazz);
    }

    public static <T> Set<T> toBeanSet(String jsonString, Class<T> clazz) {
        if (null == jsonString) {
            return null;
        }
        try {
            CollectionType setType = objectMapper.getTypeFactory().constructCollectionType(Set.class, clazz);
            return objectMapper.readValue(jsonString, setType);
        } catch (Exception e) {
            log.error("fromJsonToList-error", e);
            throw new RuntimeException(e);
        }
    }

    public static <T> Set<T> toBeanSet(Object obj, Class<T> clazz) {
        if (null == obj) {
            return null;
        }
        if (obj instanceof String) {
            return toBeanSet((String) obj, clazz);
        }
        return toBeanSet(toJson(obj), clazz);
    }

    /**
     * 判断是否是json字符串
     */
    public static Boolean isJsonStr(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.readTree(str);
            return true; // 解析成功，认为是JSON字符串
        } catch (JsonProcessingException e) {
            return false; // 解析失败，字符串不是JSON格式
        }
    }

    /**
     * 判断是否是字符串list
     */
    public static Boolean isJsonStrList(String str) {
        if (isJsonStr(str)) {
            if (str == null || str.isEmpty()) {
                return false;
            }
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                JsonNode jsonNode = objectMapper.readTree(str);
                if (jsonNode.isArray()) {
                    for (JsonNode node : jsonNode) {
                        if (!node.isTextual()) {
                            return false;
                        }
                    }
                    return true;
                }
            } catch (JsonProcessingException e) {
                throw new BizException(e.getMessage());
            }
        }
        return false;
    }

    /**
     * 判断是否是数字list
     */
    public static Boolean isJsonNumList(String str) {
        if (isJsonStr(str)) {
            if (str == null || str.isEmpty()) {
                return false;
            }
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                JsonNode jsonNode = objectMapper.readTree(str);
                if (jsonNode.isArray()) {
                    for (JsonNode node : jsonNode) {
                        if (!node.isNumber()) {
                            return false;
                        }
                    }
                    return true;
                }
            } catch (JsonProcessingException e) {
                throw new BizException(e.getMessage());
            }
        }
        return false;
    }


    /**
     * 判断是否是字符串list
     */
    public static Boolean isJsonObjList(String str) {
        if (isJsonStr(str)) {
            if (str == null || str.isEmpty()) {
                return false;
            }
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                JsonNode jsonNode = objectMapper.readTree(str);
                if (jsonNode.isArray()) {
                    for (JsonNode node : jsonNode) {
                        if (!node.isObject()) {
                            return false;
                        }
                    }
                    return true;
                }
            } catch (JsonProcessingException e) {
                throw new BizException(e.getMessage());
            }
        }
        return false;
    }
}
